package com.example.restatr.Atcivity.Park;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Adapter.CardAdapter;
import com.example.restatr.Atcivity.NewsAt;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.Atcivity.Park.Bean.ParkBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ParkAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private Banner rbt;
    private GridView card;
    private RecyclerView tccList;
    private List<CardBean> cardBeans = new ArrayList<>();
    private List<ParkBean.RowsBean> rowsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_tcc_at );


        tb = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        rbt = findViewById( R.id.banner );
        card = findViewById( R.id.card );
        tccList = findViewById( R.id.tccList );

        Tools.SetBarTitle( tb, barTitle, "附近停车场", search, ParkAt.this,false );
        Tools.SetBanner("/prod-api/api/park/rotation/list", "", "rows", "advImg", rbt,this);
        cardBeans.add( new CardBean( String.valueOf( R.drawable.ic_baseline_directions_car_24 ), "我的车辆", "" ) );
        cardBeans.add( new CardBean( String.valueOf( R.drawable.ic_baseline_equalizer_24 ), "积分兑换", "" ) );
        cardBeans.add( new CardBean( String.valueOf( R.drawable.ic_baseline_ballot_24 ), "新闻资讯", "" ) );
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) card.getLayoutParams();
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        card.setLayoutParams( layoutParams );
        card.setAdapter( new CardAdapter( "id", ParkAt.this, cardBeans ) );
        card.setOnItemClickListener( (parent, view, position, id) -> {
            switch (view.getTag().toString()) {
                case "我的车辆":
                    startActivity( new Intent( ParkAt.this, MyCarAt.class ) );
                    break;
                case "积分兑换":
                    startActivity( new Intent( this, ScoreTopAt.class ) );
                    break;
                case "新闻资讯":
                    Intent intent = new Intent( ParkAt.this, NewsAt.class );
                    intent.putExtra( "Link", "/prod-api/api/park/press/category/list" );
                    intent.putExtra( "info", "tcc" );
                    intent.putExtra( "NewsLink", "/prod-api/api/park/press/press/list?type=" );
                    intent.putExtra( "LikeLink", "/prod-api/api/park/press/press/like/" );
                    startActivity( intent );
                    break;
            }
        } );
        new Thread( () -> {
            ParkBean parkBean = new Gson().fromJson( Tools.Get( "/prod-api/api/park/lot/list", "" ), ParkBean.class );
            rowsBeanList = parkBean.getRows().stream().sorted( Comparator.comparing( ParkBean.RowsBean::getDistance ) ).collect( Collectors.toList() );
            runOnUiThread( () -> tccList.setAdapter( new tccAdapter() ) );
        } ).start();
    }

    private String getOpen(String open) {
        String s = null;
        if (open.equals( "Y" )) {
            s = "对外开放";
        } else {
            s = "不对外开放";
        }
        return s;
    }

    class tccAdapter extends RecyclerView.Adapter<tccAdapter.tccViewHolder> {

        @NonNull
        @Override
        public tccViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new tccViewHolder( getLayoutInflater().inflate( R.layout.zfz_list_item, parent, false ) );
        }

        @Override
        public void onBindViewHolder(@NonNull tccViewHolder holder, int position) {
            Glide.with( ParkAt.this )
                    .load( Tools.ip + rowsBeanList.get( position ).getImgUrl() )
                    .placeholder( R.drawable.bj )
                    .error( R.drawable.ic_baseline_terrain_24 )
                    .transform( new RoundedCorners( 15 ) )
                    .into( holder.tupian );
            holder.name.setText( rowsBeanList.get( position ).getParkName() );
            holder.diz.setText( rowsBeanList.get( position ).getAddress() );
            holder.phone.setText( "开放状态：" + getOpen( rowsBeanList.get( position ).getOpen() ) );
            holder.jg.setText( rowsBeanList.get( position ).getDistance() + "m" );
            holder.mj.setText( "价格：" + rowsBeanList.get( position ).getRates() + "/小时" );
            holder.hux.setText( "车位：" + rowsBeanList.get( position ).getVacancy() + "/" + rowsBeanList.get( position ).getAllPark() );

            holder.RL.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( ParkAt.this, ParkInfoAt.class );
                    intent.putExtra( "parkName", rowsBeanList.get( position ).getParkName() );
                    intent.putExtra( "vacancy", rowsBeanList.get( position ).getVacancy() );
                    intent.putExtra( "priceCaps", rowsBeanList.get( position ).getPriceCaps() );
                    intent.putExtra( "imgUrl", rowsBeanList.get( position ).getImgUrl() );
                    intent.putExtra( "rates", rowsBeanList.get( position ).getRates() );
                    intent.putExtra( "address", rowsBeanList.get( position ).getAddress() );
                    intent.putExtra( "distance", rowsBeanList.get( position ).getDistance() );
                    intent.putExtra( "allPark", rowsBeanList.get( position ).getAllPark() );
                    intent.putExtra( "open", holder.phone.getText().toString().substring( 5 ) );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeanList.size();
        }

        class tccViewHolder extends RecyclerView.ViewHolder {
            private RelativeLayout RL;
            private ImageView tupian;
            private TextView name;
            private TextView diz;
            private TextView phone;
            private TextView mj;
            private TextView hux;
            private TextView jg;

            public tccViewHolder(@NonNull View itemView) {
                super( itemView );
                RL = itemView.findViewById( R.id.ddInfo );
                tupian = itemView.findViewById( R.id.tupian );
                name = itemView.findViewById( R.id.name );
                diz = itemView.findViewById( R.id.diz );
                phone = itemView.findViewById( R.id.phone );
                mj = itemView.findViewById( R.id.mj );
                hux = itemView.findViewById( R.id.hux );
                jg = itemView.findViewById( R.id.jg );
            }
        }
    }
}